package com.asset.salesforce.service;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.asset.salesforce.dao.UserShiroDaoImpl;
import com.asset.salesforce.domain.Employee;
import com.asset.salesforce.domain.User;

@Service
@Transactional(readOnly=true)
public class UserShiroService {
	
	private UserShiroDaoImpl userShiroDaoImpl =null;
	
	public UserShiroDaoImpl getUserShiroDaoImpl() {
		return userShiroDaoImpl;
	}
	@Autowired
	public void setUserShiroDaoImpl(UserShiroDaoImpl userShiroDaoImpl) {
		this.userShiroDaoImpl = userShiroDaoImpl;
	}
	@Transactional(readOnly=false)
	public User getCurrentUser() {
        final Integer currentUserId = (Integer) SecurityUtils.getSubject().getPrincipal();
        if( currentUserId != null ) {
            return getUserById(currentUserId);
        } else {
            return null;
        }
    }
	@Transactional(readOnly=false)
	public void editUser(User user){
		userShiroDaoImpl.editUser(user);
	}
	
	@Transactional(readOnly=false)
	public void addUser(User user) {
		userShiroDaoImpl.addUser(user);
	}

	@Transactional(readOnly=false)
	public void deleteUser(User user) {
		userShiroDaoImpl.deleteUser(user);
	}
	

	@Transactional(readOnly=true)
	public List<User> getAllUsers(Employee companyId) {
	 return	userShiroDaoImpl.getAllUsers(companyId);
	}

	@Transactional(readOnly=true)
	public User getUserById(Integer userId) {
		 return	userShiroDaoImpl.getUserById((Integer)SecurityUtils.getSubject().getPrincipal());
	}

	@Transactional(readOnly=true)
	public User findUser(String username) {
		 return	userShiroDaoImpl.findUser(username);
	}

}
